home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 8: LINUX Games
/
Linux Cubed Series 8 - LINUX Games.iso
/
games
/
video
/
thrust-s.53
/
thrust-s
/
thrust
/
datasrc
/
txt2c.c
< prev
Wrap
C/C++ Source or Header
|
1995-10-13
|
2KB
|
106 lines
/* Written by Peter Ekberg, peda@lysator.liu.se */
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#define BUFSIZE (2050)
char buf[BUFSIZE];
void quit(char *str)
{
perror(str);
exit(1);
}
int writebuf(int flag)
{
int stat,len,i;
char out[3];
if(!flag) {
if(printf("\"") != 1)
return(1);
len=strlen(buf);
if(len)
if(buf[len-1]=='\n')
buf[--len]=0;
for(i=0; i<len; i++) {
switch(buf[i]) {
case '\t':
strcpy(out,"\\t");
break;
case '\b':
strcpy(out,"\\b");
break;
case '\f':
strcpy(out,"\\f");
break;
case '\'':
strcpy(out,"\\\'");
break;
case '\"':
strcpy(out,"\\\"");
break;
case '\\':
strcpy(out,"\\\\");
break;
default:
out[0]=buf[i];
out[1]=0;
}
stat=fputs(out,stdout);
if(stat==EOF)
return(1);
if(stat<0)
return(1);
}
if(printf("\"") != 1)
return(1);
}
else {
if(printf("NULL") != 4)
return(1);
}
return(0);
}
int main(int argc, char *argv[])
{
char *stat;
int end=0;
if(argc!=2) {
fprintf(stderr, "%s: Usage '%s variable_name'\n",
argv[0],
argv[0]);
exit(1);
}
if(printf("\n#include <stddef.h>\n\nchar *%s[] = {\n", argv[1]) != (35 + strlen(argv[1])))
quit(argv[0]);
while(!end) {
stat=fgets(buf, BUFSIZE, stdin);
if(stat==NULL) {
if(ferror(stdin))
quit(argv[0]);
end=1;
if(writebuf(1))
quit(argv[0]);
}
else {
if(writebuf(0))
quit(argv[0]);
if(printf(",\n") != 2)
quit(argv[0]);
}
}
if(printf(" };\n") != 4)
quit(argv[0]);
return(0);
}